package net.loststone

/**
 * Controlador per les explicacions dels indicadors economics.
 */
class EconInfoExplicacioController {

    def authenticationService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


    def onlyLoggedInUsers = {
        if (!authenticationService.isLoggedIn(request)) {
            redirect(controller:"sauron", action: "index", params: params)
        }
    }

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        onlyLoggedInUsers();
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [econInfoExplicacioInstanceList: EconInfoExplicacio.list(params), econInfoExplicacioInstanceTotal: EconInfoExplicacio.count()]
    }

    def create = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = new EconInfoExplicacio()
        econInfoExplicacioInstance.properties = params
        return [econInfoExplicacioInstance: econInfoExplicacioInstance]
    }

    def save = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = new EconInfoExplicacio(params)
        if (econInfoExplicacioInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), econInfoExplicacioInstance.id])}"
            redirect(action: "show", id: econInfoExplicacioInstance.id)
        }
        else {
            render(view: "create", model: [econInfoExplicacioInstance: econInfoExplicacioInstance])
        }
    }

    def show = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = EconInfoExplicacio.get(params.id)
        if (!econInfoExplicacioInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
            redirect(action: "list")
        }
        else {
            [econInfoExplicacioInstance: econInfoExplicacioInstance]
        }
    }

    def edit = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = EconInfoExplicacio.get(params.id)
        if (!econInfoExplicacioInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [econInfoExplicacioInstance: econInfoExplicacioInstance]
        }
    }

    def update = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = EconInfoExplicacio.get(params.id)
        if (econInfoExplicacioInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (econInfoExplicacioInstance.version > version) {
                    
                    econInfoExplicacioInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio')] as Object[], "Another user has updated this EconInfoExplicacio while you were editing")
                    render(view: "edit", model: [econInfoExplicacioInstance: econInfoExplicacioInstance])
                    return
                }
            }
            econInfoExplicacioInstance.properties = params
            if (!econInfoExplicacioInstance.hasErrors() && econInfoExplicacioInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), econInfoExplicacioInstance.id])}"
                redirect(action: "show", id: econInfoExplicacioInstance.id)
            }
            else {
                render(view: "edit", model: [econInfoExplicacioInstance: econInfoExplicacioInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
            redirect(action: "list")
        }
    }


    def delete = {
        onlyLoggedInUsers();
        def econInfoExplicacioInstance = EconInfoExplicacio.get(params.id)
        if (econInfoExplicacioInstance) {
            try {
                econInfoExplicacioInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'econInfoExplicacio.label', default: 'EconInfoExplicacio'), params.id])}"
            redirect(action: "list")
        }
    }
}
